package io.kiki.sba.registry.server.metadata.bootstrap;

import io.kiki.sba.registry.server.metadata.bootstrap.config.MultiClusterMetaServerConfig;
import io.kiki.sba.registry.server.metadata.bootstrap.config.MultiClusterMetaServerConfigBean;
import io.kiki.sba.registry.server.metadata.multi.cluster.MultiClusterSlotsSyncerImpl;
import io.kiki.sba.registry.server.metadata.multi.cluster.remote.RemoteClusterCentralControlExchanger;
import io.kiki.sba.registry.server.metadata.multi.cluster.remote.RemoteClusterSlotSyncHandler;
import io.kiki.sba.registry.server.metadata.resource.MultiClusterSyncResource;
import io.kiki.sba.registry.server.metadata.resource.MultiDatumResource;
import io.kiki.sba.registry.server.shared.remoting.AbstractServerHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Collection;


@Configuration
@EnableConfigurationProperties
public class MultiClusterMetaServerConfiguration {

    @Configuration
    public static class MultiClusterConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public MultiClusterMetaServerConfig multiClusterMetaServerConfig() {
            return new MultiClusterMetaServerConfigBean();
        }

        @Bean
        public MultiClusterSlotsSyncerImpl multiClusterSlotsSyncer() {
            return new MultiClusterSlotsSyncerImpl();
        }

        @Bean
        public RemoteClusterCentralControlExchanger remoteClusterMetaExchanger() {
            return new RemoteClusterCentralControlExchanger();
        }

        @Bean
        public MultiClusterSyncResource multiClusterSyncResource() {
            return new MultiClusterSyncResource();
        }

        @Bean
        public MultiDatumResource multiDatumResource() {
            return new MultiDatumResource();
        }
    }

    @Configuration
    public static class MultiClusterRemotingConfiguration {

        @Bean(name = "remoteMetaServerHandlers")
        public Collection<AbstractServerHandler> metaServerHandlers() {
            Collection<AbstractServerHandler> list = new ArrayList<>();
            list.add(remoteClusterSlotSyncHandler());
            return list;
        }

        @Bean
        public RemoteClusterSlotSyncHandler remoteClusterSlotSyncHandler() {
            return new RemoteClusterSlotSyncHandler();
        }
    }
}
